package com.noah.note.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.noah.note.R;
import com.noah.note.adapter.HomePagerAdapter;
import com.noah.note.global.BaseActivity;
import com.noah.note.global.BasePresenter;
import com.noah.note.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主界面
 */
public class HomeActivity extends BaseActivity {
    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    @BindView(R.id.tvTest)
    TextView tvTest;
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    private HomePagerAdapter pagerAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private String[] titles = {"日记","收藏","我的日记","设置"};
    private int[] imgs = {R.drawable.selector_home, R.drawable.selector_home, R.drawable.selector_home, R.drawable.selector_home};

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initView();

        //SocketManager.getInstance().testSocket(tvTest);
//        SocketManager.getInstance().connectSocket();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void initView() {

        fragmentList.add(new NoteListFragment());
        fragmentList.add(new MarkFragment());
        fragmentList.add(new MyNoteFragment());
        fragmentList.add(new SettingFragment());
        pagerAdapter = new HomePagerAdapter(this, getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(pagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView ivTabBottom = tab.getCustomView().findViewById(R.id.ivTabBottom);
                ivTabBottom.setImageResource(R.drawable.ic_home_black);
                mTitleBar.setTitle(titles[tab.getPosition()]);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView ivTabBottom = tab.getCustomView().findViewById(R.id.ivTabBottom);
                ivTabBottom.setImageResource(R.drawable.ic_home_white);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab_item = mTabLayout.getTabAt(i);
            tab_item.setCustomView(pagerAdapter.getCustomTabView(i));
        }
    }


}

